package com.yami.shop.service.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.model.Product;
import com.yami.shop.bean.pt.YxPtsp;
import com.yami.shop.common.util.PageAdapter;
import com.yami.shop.dao.ProductMapper;
import com.yami.shop.dao.YxPtspMapper;
import com.yami.shop.service.IYxPtspService;
import com.yami.shop.service.ProductService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class YxPtspServiceImpl extends ServiceImpl<YxPtspMapper,YxPtsp> implements IYxPtspService {
    @Autowired
    private YxPtspMapper yxPtspMapper;
    @Autowired
    private ProductMapper productMapper;

    /**
     * 查询拼团商品信息
     *
     * @param ptspid 拼团商品ID
     * @return 拼团商品信息
     */
    @Override
    public YxPtsp selectYxPtspById(Long ptspid)
    {
        return yxPtspMapper.selectYxPtspById(ptspid);
    }

    /**
     * 查询拼团商品列表
     *
     * @param yxPtsp 拼团商品信息
     * @return 拼团商品集合
     */
    @Override
    public IPage<YxPtsp> selectYxPtspList(Page<YxPtsp> page, YxPtsp yxPtsp)
    {
        //记录
        List<YxPtsp> list = yxPtspMapper.selectYxPtspList(new PageAdapter(page),yxPtsp);
        if(CollectionUtils.isNotEmpty(list)){
            for(YxPtsp yxPtsp1 : list){
                Long prodId = yxPtsp1.getGoodsid();
                Product prod =productMapper.getProdcut(prodId);
                if(prod!=null){
                    yxPtsp1.setGoodsImg(prod.getPic());
                    yxPtsp1.setGoodsName(prod.getProdName());
                    yxPtsp1.setGoodsPrice(new BigDecimal(prod.getPrice()));
                }
            }

        }
        page.setRecords(list);
        page.setSize(yxPtspMapper.getYxPtspListNumber(yxPtsp));
        return page;
    }

    @Override
    public List<YxPtsp> getYxPtspList(YxPtsp yxPtsp) {
        return yxPtspMapper.getYxPtspList(yxPtsp);
    }

    /**
     * 新增拼团商品
     *
     * @param yxPtsp 拼团商品信息
     * @return 结果
     */
    @Override
    public int insertYxPtsp(YxPtsp yxPtsp)
    {
        return yxPtspMapper.insertYxPtsp(yxPtsp);
    }

    /**
     * 修改拼团商品
     *
     * @param yxPtsp 拼团商品信息
     * @return 结果
     */
    @Override
    public int updateYxPtsp(YxPtsp yxPtsp)
    {
        return yxPtspMapper.updateYxPtsp(yxPtsp);
    }

    /**
     * 删除拼团商品对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteYxPtspByIds(String ids)
    {
        return yxPtspMapper.deleteYxPtspByIds(Convert.toStrArray(ids));
    }

    /**
     * 查询拼团商品信息
     *	ltb
     * @param ptglid 拼团商品ID
     * @return 拼团商品信息
     */
    public YxPtsp selectYxPtspByPtglid(Long ptglid){
        return yxPtspMapper.selectYxPtspByPtglid(ptglid);
    }

    /**
     * 查询拼团商品信息
     *	ltb
     * @param goodsid 拼团商品ID
     * @return 拼团商品信息
     */
    public YxPtsp selectYxPtspBygoodsid(Long goodsid){
        return yxPtspMapper.selectYxPtspBygoodsid(goodsid);
    }

    @Override
    public YxPtsp getYxPtspbyProdId(Long prodId) {
        return yxPtspMapper.getYxPtspbyProdId(prodId);
    }


}
